package com.wanxi.service.impl;

import com.wanxi.dao.CompanyDao;
import com.wanxi.dao.impl.CompanyDaoImpl;
import com.wanxi.dto.CompanyDto;
import com.wanxi.model.CompanyModel;
import com.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyDaoImpl();

    @Override
    public CompanyDto getCompanyDto(CompanyModel companyModel) {

        /**
         * 1.首先判断redis里面是否有数据
         * 2.如果有，则直接取redis里面的数据
         * 3.如果没有，则取数据库里面的数据后将数据保存到redis
         */
        Jedis jedis = new Jedis();
        CompanyDto companyDto = new CompanyDto();
        boolean logo = jedis.exists("logo");
        if (logo) {
//            companyDto = (CompanyDto) jedis.get("aa");
            companyDto = CompanyDto.builder()
                    .logo(jedis.get("logo"))
                    .phone(jedis.get("phone"))
                    .email(jedis.get("email"))
                    .version(jedis.get("version"))
                    .address(jedis.get("address"))
                    .linePhone(jedis.get("linePhone"))
                    .build();
            System.out.println("redis");
        } else {
//            取数据库的值
            companyDto = companyDao.getCompanyDto(companyModel);
//   将数据保存到redis
//            jedis.set("aa", companyDto);
//            jedis.set("logo", companyDto.getLogo());
            jedis.setex("logo",100, companyDto.getLogo());
            jedis.set("phone", companyDto.getPhone());
            jedis.set("email", companyDto.getEmail());
            jedis.set("address", companyDto.getAddress());
            jedis.set("version", companyDto.getVersion());
            jedis.set("linePhone", companyDto.getLinePhone());
            System.out.println("mysql");
        }
        return companyDto;
    }
}
